package com.hp.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.hp.dao.LinkDao;
import com.hp.entity.Link;
import com.hp.result.DataGridResultInfo;
import com.hp.service.LinkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Transactional
@Service
public  class LinkServiceImpl implements LinkService {
    @Autowired
    private LinkDao linkDao;
    @Transactional(readOnly = true)
    @Override
    public List<Link> findAll() throws  Exception{
        return linkDao.findAll();
    }
    @Transactional(readOnly = true)
    @Override
    public DataGridResultInfo findlinkList(int page, int rows) throws Exception {
        Page<Link> startPage = PageHelper.startPage(page, rows);//使用分页插件方法
        linkDao.findAll();//插件方法必须在查询数据之前
        //封装数据
        DataGridResultInfo dataGridResultInfo = new DataGridResultInfo();
        dataGridResultInfo.setRows(startPage.getResult());//当前查询的数据
        dataGridResultInfo.setTotal((int) startPage.getTotal());//总页数
        return dataGridResultInfo;
    }
    @Transactional(readOnly = true)
    @Override
    public Link findById(String id) throws Exception {
        return linkDao.findByIdLink(id);
    }

    @Override
    public void updateLink(Link link) throws Exception {
        linkDao.update(link);
    }
}
